package com.ita.tba.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.tba.po.Cart;
import com.ita.tba.po.Dish;
import com.ita.tba.po.DishCache;
import com.ita.tba.po.User;
import com.ita.tba.service.Controller;
import com.ita.tba.service.impl.ControllerImpl;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Controller controller = new ControllerImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String email = (String) session.getAttribute("email");
		
		Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
		dishCache = DishCache.dishCache;
		List<Dish> dishes = new ArrayList<Dish>();
		for (Integer did : dishCache.keySet()) {
			dishes.add(dishCache.get(did));
		}
		if (dishCache != null) {
			request.setAttribute("dishes", dishes);
		}

		
		int uid = controller.getUid(email);
		Cart cart = controller.getThisCart(uid);
		Map<Integer, Integer> didNum = cart.getDidNum();
		Map<String, Integer> dnameNum = new HashMap<String, Integer>();
		for (Integer did : didNum.keySet()) {
			dnameNum.put(dishCache.get(did).getDname(), didNum.get(did));
		}
		double totalPrice = cart.getTotalPrice();
		dnameNum.put("TotalPrice RMB:", (int) totalPrice);
		request.setAttribute("cart", dnameNum);

		request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String email = request.getParameter("email");
		String password = request.getParameter("password");

		HttpSession session = request.getSession();
		boolean isEmailExist = controller.isEmailExist(email);

		boolean isPasswordCorrect = controller.isPasswordCorrect(email, password);

		Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
		dishCache = DishCache.dishCache;
		List<Dish> dishes = new ArrayList<Dish>();
		for (Integer did : dishCache.keySet()) {
			dishes.add(dishCache.get(did));
		}
		if (dishCache != null) {
			request.setAttribute("dishes", dishes);
		}

		if (isPasswordCorrect) {
			session.setAttribute("email", email);
			session.setAttribute("password", password);
			int uid = controller.getUid(email);
			Cart cart = controller.getThisCart(uid);
			Map<Integer, Integer> didNum = cart.getDidNum();
			Map<String, Integer> dnameNum = new HashMap<String, Integer>();
			for (Integer did : didNum.keySet()) {
				dnameNum.put(dishCache.get(did).getDname(), didNum.get(did));
			}
			double totalPrice = cart.getTotalPrice();
			dnameNum.put("TotalPrice RMB:", (int) totalPrice);
			request.setAttribute("cart", dnameNum);
		} else {
			session.setAttribute("error", "Password error!");
		}

		if (!isEmailExist) {
			session.setAttribute("error", "Email not exist!");
		}
		request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
	}

}
